const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const moment = require('moment');
moment.locale('zh-cn');
// 分类管理
const categorySchema = new Schema({
  name: String,
  children: [],
  articles: [{ type: Schema.Types.ObjectId, ref: 'article' }], // type为Article文章的id
  createTime: {
    type: Date,
    default: Date.now
  },
  updateTime: {
    type: Date,
    default: Date.now
  }
}, {
  versionKey: false,
  timestamps: { createdAt: 'createTime', updatedAt: 'updateTime' }
});
categorySchema.set('toJSON', { getters: true, virtuals: true });
categorySchema.set('toObject', { getters: true, virtuals: true });
categorySchema.path('createTime').get(function (v) {
  return moment(v).format("YYYY-MM-DD HH:mm:ss");
});
categorySchema.path('updateTime').get(function (v) {
  return moment(v).format("YYYY-MM-DD HH:mm:ss");
});

//module.exports 指向新的对象时，exports 断开了与 module.exports 的引用
//，那么通过 exports = module.exports 让 exports 重新指向 module.exports 即可。
exports = module.exports = mongoose.model('category', categorySchema);
